package com.session;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

/**
 * Created by Greg.chen on 2016-03-23.
 */
public class Session {

    /**
     * 绑定对象key
     */
    public static AttributeKey<Object> ATTACHMENT_KEY = AttributeKey.valueOf("ATTACHMENT_KEY");

    /**
     * 实际会话对象
     */
    private Channel channel;


    public Session(Channel channel) {

        this.channel = channel;
    }


    public Object getAttachment() {

        return channel.attr(ATTACHMENT_KEY).get();
    }


    public void setAttachment(Object attachment) {

        channel.attr(ATTACHMENT_KEY).set(attachment);
    }


    public void removeAttachment() {

        channel.attr(ATTACHMENT_KEY).remove();
    }

    public void write(Object message) {

        channel.writeAndFlush(message);
    }

    public boolean isConnected() {
        return channel.isActive();
    }


    public void close() {

        channel.close();
    }

}
